/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * TieBreakTrainingCoach.java
 *
 * Created on 23-03-2011, 10:54:42
 */

package tiebreak.UI;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultListModel;
import tiebreak.BE.Member;
import tiebreak.DAL.DalcTraining;


/**
 *
 * @author henrikhansen
 */
public class TieBreakTrainingCoach extends javax.swing.JPanel {

    int[] coacheID;
    String coachFirst;
    String coachLast;
    String[] teams;
    /** Creates new form TieBreakTrainingCoach */
    public TieBreakTrainingCoach() throws SQLException {
        initComponents();
        lstCoaches.setModel(createListModel());
        MouseListener ml = new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e)
            {
                try {
                    System.out.println(lstCoaches.getSelectedIndex());
                    assignCoachToTeam(coachFirst, coachLast, coacheID[lstCoaches.getSelectedIndex()]);
                } catch (SQLException ex) {
                    Logger.getLogger(TieBreakTrainingCoach.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        };


        MouseListener assign = new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e)
            {
                assignCoach.setEnabled(true);
            }
        };
        assignCoach.addMouseListener(ml);
        lstCoaches.addMouseListener(assign);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblCoaches = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        lstCoaches = new javax.swing.JList();
        jLabel1 = new javax.swing.JLabel();
        lblCoachTeams = new javax.swing.JLabel();
        assignCoach = new javax.swing.JButton();

        lblCoaches.setFont(new java.awt.Font("Lucida Grande", 1, 13));
        lblCoaches.setText("Coaches:");

        jScrollPane1.setViewportView(lstCoaches);

        jLabel1.setFont(new java.awt.Font("Lucida Grande", 1, 13));
        jLabel1.setText("Teams:");

        assignCoach.setText("Assign coache to team");
        assignCoach.setEnabled(false);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 398, Short.MAX_VALUE)
                    .add(lblCoaches)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                        .add(jLabel1)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(lblCoachTeams)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 155, Short.MAX_VALUE)
                        .add(assignCoach)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(lblCoaches)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 202, Short.MAX_VALUE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel1)
                    .add(lblCoachTeams)
                    .add(assignCoach))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton assignCoach;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblCoachTeams;
    private javax.swing.JLabel lblCoaches;
    private javax.swing.JList lstCoaches;
    // End of variables declaration//GEN-END:variables

    private DefaultListModel createListModel() throws SQLException {

        DefaultListModel m_model = new DefaultListModel();
        for(Member inList : DalcTraining.getAllCoaches())
        {
            coacheID = new int[DalcTraining.getAllCoaches().size()];
            coachFirst = inList.getFirstName();
            coachLast = inList.getLastName();
            int count = 0;
            m_model.addElement(inList.getLastName()+", "+inList.getFirstName());
            coacheID[count] = inList.getMemberID();
            count++;
        }
        return m_model;
    }


    public void assignCoachToTeam(String coachFirst, String coachLast, int coachid) throws SQLException
    {
        new AssignCoachToTeam(coachFirst, coachLast, coachid);
    }

}
